package com.app.micaihu.view.user.usertask;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.infor.GetTaskBean;
import com.app.micaihu.configure.ConfigKeyConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.AppConfigManager;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

/**
 * Created by linkai on 2017/7/4.
 */
public class InvicodeActivity extends BaseOldActivity implements View.OnClickListener {

    String invicode;
    private TextView officialCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addContentView(R.layout.activity_invicode);
        setTitle("输入邀请码");

        final EditText invicodeView = (EditText) findViewById(R.id.et_phonenum);
        final TextView submit = (TextView) findViewById(R.id.tv_next);
        officialCode = (TextView) findViewById(R.id.inviteCode);

        String s = AppConfigManager.getInstance().readConfig(ConfigKeyConstant.officialInviteCode, "");
        if(!TextUtils.isEmpty(s)){
            officialCode.setText("无人邀请可填写官方邀请码: "+s);
        }


        submit.setOnClickListener(this);
        invicodeView.addTextChangedListener(new TextWatcher(){

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                invicode = invicodeView.getText().toString().trim();
                if(TextUtils.isEmpty(invicode)){
                    submit.setEnabled(false);
                }else{
                    submit.setEnabled(true);
                }

            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v == null){
            return;
        }
        submit();
    }

    /**
     * 提交
     */
    private void submit(){

        if(!UserInforManager.getInstance().isLogin()){
            ToastUtils.showText("请先登录");
            UserInforManager.getInstance().toLoginPage(this);
            return;
        }
        ParamMap parmsMap = new ParamMap();
        parmsMap.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());
        parmsMap.put("inviteCode", invicode + "");
        HttpUtils.ParmsMapMD5(parmsMap);
        HttpListener<DataBean<GetTaskBean>> httpListener = new HttpListener<DataBean<GetTaskBean>>() {

            @Override
            public void onStart() {

                DialogUtils.getInstance().showProgressDialog(InvicodeActivity.this,"正在提交中...");
            }

            @Override
            public void onError(VolleyError error) {
                DialogUtils.getInstance().dismissProgressDialog();
                ToastUtils.makeText(AppApplication.getInstance().getString(R.string.neterror));
            }

            @Override
            public void onSuccess(DataBean<GetTaskBean> response) {
                if (response.noError() && response.getData() != null
                        && response.getData().getIsGetTask("填写邀请码")) {
                    if(UserInforManager.getInstance().isLogin()){
                        UserInforManager.getInstance().getUserInfor().getTask().setIsInvited("1");
                    }

                    //刷新任务
                    UserInforManager.getInstance().getUserTask(mActivity, true);
                }else if(response != null){
                    ToastUtils.makeText(response.getNnderstoodMsg());
                }
            }
        };
        //请求网络
        addPostCacheRequest(URIContant.SUBMITINVITECODE, new TypeToken<DataBean<GetTaskBean>>() {
        }.getType(), parmsMap, httpListener);
    }
}
